package com.icss.train_sys_back.controller.home;

import com.icss.train_sys_back.entity.ResponseResult;
import com.icss.train_sys_back.entity.ResultEnum;
import com.icss.train_sys_back.entity.home.CreateOrderDTO;
import com.icss.train_sys_back.entity.home.OrderEntity;
import com.icss.train_sys_back.entity.ticket_manage.ChangeTicketEntity;
import com.icss.train_sys_back.entity.ticket_manage.TicketEntity;
import com.icss.train_sys_back.service.home.OrderService;
import com.icss.train_sys_back.service.system_service.UserService;
import com.icss.train_sys_back.service.ticket_manage.TicketManageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author
 * @createTime 2025/10/13 10:30
 */
@RestController
@RequestMapping("/home/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private TicketManageService ticketManageService;

    //创建订单
    @PostMapping("/createOrder")
    public ResponseResult createOrder(@RequestBody CreateOrderDTO createOrder) {
        log.info("创建订单  createOrder = {}", createOrder);
        return orderService.createOrder(createOrder);
    }

    // 获取订单信息接口
    @GetMapping("/getOrderInfo")
    public ResponseResult getOrderInfo(@RequestParam(required = false) String orderId) {
        //入参
        log.info("{}", orderId);
        // 直接调用Service层方法
        OrderEntity orderEntity = orderService.getOrderInfo(orderId);
        log.info("查询结果：{}", orderEntity);
        // 使用统一响应结果类封装数据
        return orderEntity != null ? ResponseResult.success(orderEntity) : ResponseResult.fail(ResultEnum.ORDER_NOT_FOUND);
    }


    //取消订单：传回来一个订单号
    @PutMapping("/cancelOrder/{orderId}")
    public ResponseResult cancelOrder(@PathVariable("orderId") String orderId) {
        log.info("取消订单  orderId = {}", orderId);
        boolean res = orderService.cancelOrder(orderId);
        return res ? ResponseResult.success() : ResponseResult.fail(ResultEnum.FAIL);
    }

    //根据订单Id和车票Id  退票
    @PutMapping("/cancelTicket/{ticketId}/{orderId}/{userId}")
    public ResponseResult refundTicket(@PathVariable("ticketId") String ticketId,
                                       @PathVariable("orderId") String orderId,
                                       @PathVariable("userId") Integer userId) {
        log.info("根据订单Id和车票Id  退票   ticketId  = {} ,orderId = {} , userId = {}", ticketId, orderId, userId);
        return userService.refundTicketBiTicketId(ticketId,orderId, userId);

    }

    //改签车次查询
    @GetMapping("/findTrainNumberByDateAndRoute")
    public ResponseResult findTrainNumberByDateAndRoute(@RequestParam Integer startStationId,
                                                        @RequestParam Integer endStationId,
                                                        @RequestParam String date,
                                                        @RequestParam(required = false) String timeRange) {
        log.info("改签车次查询  startStationId = {}，endStationId = {}，date = {}，timeRange = {}", startStationId, endStationId, date, timeRange);
        List<TicketEntity> ticketEntityList = orderService.getNewTrainTickets(startStationId, endStationId, date, timeRange);
        return ResponseResult.success(ticketEntityList);

    }

    //改签请求
    @PutMapping("/changeTicket/{userId}")
    public ResponseResult changeTicket(@PathVariable("userId") Integer userId,
                                       @RequestBody ChangeTicketEntity changeTicket) {
        log.info("改签请求   userId = {} ,  changeTicket  = {}", userId, changeTicket);
        return orderService.changeTicket(userId, changeTicket);
    }

}
